package jihe_set_of_APIdemo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author ChenYongHao
 * @date 2023/4/28 9:49
 * @desc Collection接口
 */
public class Collection_APIdemo {


    public static void main(String[] args) {
        // 创建集合
        Collection<String> collection = new ArrayList<String>();

        // 添加元素
        collection.add("张三");
        collection.add("张三");
        collection.add("李四");
        collection.add("李四");
        collection.add("王五");
        collection.add("王五");
        System.out.println(collection.toString());

        // 移除元素
        collection.remove("张三");
        System.out.println(collection.toString());

        // 判断元素
        boolean isContains = collection.contains("李四");
        System.out.println(isContains);

        // 判断集合是否为空
        boolean isEmpty = collection.isEmpty();
        System.out.println(isEmpty);

        // 获取集合元素个数
        int size = collection.size();
        System.out.println(size);

        // 用迭代器遍历集合
        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        // 用增强for循环遍历
        for (String s : collection) {
            System.out.println(s);
        }

        // 添加另外集合元素
        Collection<String> c = new ArrayList<String>();
        c.add("小可爱");
        c.add("大可爱");
        collection.addAll(c);
        System.out.println(collection.toString());

        // 清空集合所有元素
        collection.clear();
        System.out.println(collection.toString());
    }

}

